package com.atguigu.gulimall.order.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class MyRabbitConfig {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 定制RabbitTemplate
     *     @PostConstruct :  MyRabbitMQConfig对象创建完以后就会执行带有@PostConstruct的方法
     *     1、服务收到消息就回调
     *          1、spring.rabbitmg.publisher-confirms=true
     *          2、设置确认回调Confirmcallback
     *     2、消息正确抵达队列进行回调
     */
    @PostConstruct
    public void initRabbitTemplate() {
        //设置确认回调
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            /**
             * 1,只要抵达服务器交换机,ack就是true
             * @param correlationData 当前消息的唯一关联数据(消息的唯一id)
             * @param ack 消息是否成功收到
             * @param cause 失败的原因
             */
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                /**
                 * 1,防止消息丢失
                 *      做好消息确认机制(pulisher，consumer【手动ack】)
                 *      每一个发送的消息都在数据库做好记录。定期将失败的消息再次发送一遍
                 */
                System.out.println("confirm...correlationData: " + correlationData + "==> ack: " + ack + "==> cause: " + cause);
            }
        });

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {

            /**
             * 只要消息没有投递给指定的队列，就触发这个失败回调
             * @param message   投递失败的消息详细信息
             * @param replyCode 回复的状态码
             * @param replyText 回复的文本内容
             * @param exchange  当时这个消息投递给哪个交换机
             * @param routingKey 当时这个消息用哪个路由键
             */
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                // 报错误了-修改数据库当前消息的状态->错误
                System.out.println("报错误了");
                System.out.println("Fail Message Data: " + message + "==> replyCode: " + replyCode + "==> replyText: " + replyText + "==> exchange: " + exchange + "==> routingKey: " + routingKey);
            }
        });
    }
}
